package com.cml.getway.core.netty;


import com.cml.getway.core.context.HttpRequestWrapper;
import com.cml.getway.core.netty.processor.NettyProcessor;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 姓名：陈茂林
 * 时间：2023/12/20
 * 描述：
 */
@Slf4j
public class NettyHttpServerHandler extends ChannelInboundHandlerAdapter {
    private  NettyProcessor nettyProcessor;
    public NettyHttpServerHandler(NettyProcessor nettyProcessor) {
        this.nettyProcessor=nettyProcessor;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
       if(msg instanceof HttpRequest){
           FullHttpRequest request = (FullHttpRequest)msg;
           HttpRequestWrapper httpRequestWrapper =new HttpRequestWrapper();
           httpRequestWrapper.setFullHttpRequest(request);
           httpRequestWrapper.setCtx(ctx);
           nettyProcessor.process(httpRequestWrapper);
       }else{
            log.error("{}:该请求不是一个http请求",msg);
           boolean release =ReferenceCountUtil.release(msg);
           if(!release) {
               log.error("释放资源失败");
           }
       }
    }
}
